package com.hdlang.assists.ad.dao.step

import android.companion.AssociationInfo
import com.blankj.utilcode.util.AppUtils
import com.hdlang.assists.ad.data.DataElement
import com.hdlang.assists.ad.data.WidgetQueryEnum
import com.hdlang.assists.ad.utils.FloatWindowUtils
import com.hdlang.assists.ad.utils.GestureUtils
import com.hdlang.assists.ad.utils.LogUtils
import com.hdlang.assists.ad.utils.RandomUtils
import com.ven.assists.AssistsCore
import com.ven.assists.stepper.Step
import com.ven.assists.stepper.StepCollector
import com.ven.assists.stepper.StepImpl
import com.ven.assists.utils.NodeClassValue

// 今日头条任务
class JRNewsStep : StepImpl() {
    private val targetPkg = "com.ss.android.article.news"

    override fun onImpl(collector: StepCollector) {
        collector.next(StepTag.STEP_1) {
            val result = GestureUtils.tryBoolean(count = 3) {
                AssistsCore.home()
                GestureUtils.widgetDelay(start = 1500, end = 2000)
                AssistsCore.recentApps()
                GestureUtils.widgetDelay(start = 500, end = 1000)
                val dataElementArray = GestureUtils.cleanBackgroundElement()
                for (dataElement in dataElementArray) {
                    val clickResult = GestureUtils.click(dataElement)
                    if (clickResult) {
                        return@tryBoolean true
                    }
                }
                return@tryBoolean false
            }
            LogUtils.d("Step1 result = $result")
            if (result) {
                return@next Step.get(StepTag.STEP_2)
            }
            return@next Step.repeat
        }.next(StepTag.STEP_2) {
            FloatWindowUtils.record("打开今日头条")
            for (index in 0..10) {
                AssistsCore.home()
                AppUtils.launchApp(targetPkg)
                GestureUtils.widgetDelay(start = 1000, end = 2000)
                val result = GestureUtils.tryBoolean {
                    val packageName = AssistsCore.getPackageName()
                    LogUtils.d("当前类名：$packageName")
                    if (packageName == targetPkg) {
                        return@tryBoolean true
                    }
                    GestureUtils.widgetDelay(start = 1000, end = 2000)
                    GestureUtils.click(
                        dataElement = DataElement(
                            text = "始终允许", runDelay = false
                        )
                    )
                    GestureUtils.click(dataElement = DataElement(text = "允许", runDelay = false))
                    GestureUtils.widgetDelay(start = 1000, end = 2000)
                    return@tryBoolean false
                }
                if (result) {
                    return@next Step.get(StepTag.STEP_3)
                }
            }
            return@next Step.get(StepTag.STEP_1)
        }.next(StepTag.STEP_3) {
            val option = RandomUtils.random(2)
            when (option) {
                // 查看关注
                1 -> return@next Step.get(StepTag.STEP_4)
            }
            FloatWindowUtils.record("执行搜剧看视频")
            // 搜索，看视频
            return@next Step.get(StepTag.STEP_4)
        }.next(StepTag.STEP_4) {
            FloatWindowUtils.record("点击搜索框")
            val result = GestureUtils.click(
                dataElement = DataElement(
                    descStart = "搜索框",
                    className = NodeClassValue.TextView,
                ),
            )
            if (result && GestureUtils.existElement(
                    dataElement = DataElement(
                        text = "搜索",
                        className = NodeClassValue.Button,
                    )
                ) && GestureUtils.setInput(RandomUtils.randomSearchContent())
            ) {
                return@next Step.get(StepTag.STEP_5)
            }

            return@next Step.repeat
        }.next(StepTag.STEP_5) {
            FloatWindowUtils.record("进行搜索")
            val result = GestureUtils.click(
                dataElement = DataElement(
                    text = "搜索",
                    className = NodeClassValue.Button,
                )
            )
            if (result) {
                return@next Step.get(StepTag.STEP_6)
            }
            return@next Step.repeat
        }.next(StepTag.STEP_6) {
            val result = GestureUtils.click(
                dataElement = DataElement(
                    text = "视频",
                    className = NodeClassValue.TextView,
                ),
            )
            if (result) {
                val videoNode = GestureUtils.findWidget(
                    dataElement = DataElement(
                        text = "视频",
                        className = NodeClassValue.TextView,
                    )
                )
                if (videoNode?.isSelected == true) {
                    GestureUtils.click(
                        dataElement = DataElement(
                            text = "双排",
                            className = NodeClassValue.TextView,
                        )
                    )

                    return@next Step.get(StepTag.STEP_7)
                }
            }
            return@next Step.repeat
        }.next(StepTag.STEP_7) {
            if (GestureUtils.click(
                    dataElement = DataElement(
                        className = NodeClassValue.View, queryEnum = WidgetQueryEnum.TEXT_NOT_EMPTY
                    )
                )
            ) {
                return@next Step.get(StepTag.STEP_8)
            }
            return@next Step.repeat
        }.next(StepTag.STEP_8) {
            val delayStart = 300000
            val delayEnd = 600000
            val duration = RandomUtils.random(delayEnd - delayStart) + delayStart
            val endTime = System.currentTimeMillis() + duration
            while (endTime > System.currentTimeMillis()) {
                GestureUtils.widgetDelay(start = 5000, end = 60000)
                GestureUtils.tryBoolean {
                    return@tryBoolean GestureUtils.swipeUp(
                        x1Start = 0.4,
                        x1End = 0.6,
                        y1Start = 0.6,
                        y1End = 0.9,
                        x2Start = 0.4,
                        x2End = 0.6,
                        y2Start = 0.1,
                        y2End = 0.3,
                        durationStart = 600,
                        durationEnd = 2000,
                        runDelay = true,
                        delayStart = 600,
                        delayEnd = 2000
                    )
                }

            }
            return@next Step.get(StepTag.STEP_31)
        }.next(StepTag.STEP_11) {
            // 查看 关注列表
            val result = GestureUtils.click(
                dataElement = DataElement(
                    text = "关注",
                    className = NodeClassValue.TextView,
                ),
            )
            if (result) {
                val videoNode = GestureUtils.findWidget(
                    dataElement = DataElement(
                        text = "关注",
                        className = NodeClassValue.TextView,
                    )
                )
                if (videoNode?.isSelected == true) {
                    return@next Step.get(StepTag.STEP_12)
                }
            }
            return@next Step.repeat
        }.next(StepTag.STEP_12) {
            val itemResult = GestureUtils.click(
                dataElement = DataElement(
                    descStart = "追踪者,",
                    className = NodeClassValue.LinearLayout
                ),
            )
            if (itemResult) {
                if (GestureUtils.existElement(dataElement = DataElement(text = "TA的橱窗"))) {
                    if (GestureUtils.click(
                            dataElement = DataElement(
                                descStart = "追踪者,",
                                className = NodeClassValue.LinearLayout
                            ),
                        )
                    ) {
                        return@next Step.get(StepTag.STEP_13)
                    }
                } else if (GestureUtils.existElement(
                        dataElement = DataElement(
                            desc = "听头条",
                            className = NodeClassValue.ImageView
                        )
                    )
                ) {
                    return@next Step.get(StepTag.STEP_13)
                }
            }
            return@next Step.repeat
        }.next(StepTag.STEP_13) {
            val isReadVoice = RandomUtils.random(3) == 1
            if (isReadVoice) {
                if (GestureUtils.click(
                        dataElement = DataElement(
                            desc = "听头条",
                            className = NodeClassValue.ImageView
                        )
                    )
                ) {
                    return@next Step.get(StepTag.STEP_14)
                }
            } else {
                while (true) {
                    if (!GestureUtils.existElement(dataElement = DataElement(text = "暂无评论，点击抢首评"))) {
                        return@next Step.get(StepTag.STEP_14)
                    }
                    if (!GestureUtils.existElement(dataElement = DataElement(text = "已显示全部评论"))) {
                        return@next Step.get(StepTag.STEP_14)
                    }
                    GestureUtils.swipeUp(
                        x1Start = 0.4,
                        x1End = 0.6,
                        y1Start = 0.6,
                        y1End = 0.9,
                        x2Start = 0.4,
                        x2End = 0.6,
                        y2Start = 0.1,
                        y2End = 0.3,
                        durationStart = 600,
                        durationEnd = 2000,
                        runDelay = true,
                        delayStart = 3000,
                        delayEnd = 5000
                    )
                }
            }
            return@next Step.repeat
        }.next(StepTag.STEP_14) {
            val option = RandomUtils.random(10)
            if (option == 1) {
                // 写评论
                return@next Step.get(StepTag.STEP_21)
            } else if (option == 2) {
                // 点赞
                return@next Step.get(StepTag.STEP_25)
            }
            return@next Step.get(StepTag.STEP_31)
        }.next(StepTag.STEP_21) {
            // 写评论
            while (true) {
                if (GestureUtils.click(
                        dataElement = DataElement(
                            descStart = "评论,",
                            className = NodeClassValue.Button
                        )
                    )
                ) {
                    GestureUtils.widgetDelay(start = 500, end = 1000)
                    if (GestureUtils.existElement(dataElement = DataElement(text = "同时转发")) ||
                        GestureUtils.existElement(
                            dataElement = DataElement(
                                desc = "相册",
                                className = NodeClassValue.ImageView
                            )
                        ) ||
                        GestureUtils.existElement(
                            dataElement = DataElement(
                                desc = "话题",
                                className = NodeClassValue.ImageView
                            )
                        )
                    ) {
                        return@next Step.get(StepTag.STEP_22)
                    }
                }
            }
            return@next Step.repeat
        }.next(StepTag.STEP_22) {
            if (GestureUtils.click(
                    dataElement = DataElement(
                        className = NodeClassValue.LinearLayout,
                        depth = 7,
                        drawingOrder = 1
                    )
                )
            ) {
                return@next Step.get(StepTag.STEP_31)
            }
            return@next Step.repeat
        }.next(StepTag.STEP_25) {
            // 点赞
            if (GestureUtils.existElement(
                    dataElement = DataElement(
                        className = NodeClassValue.Button,
                        desc = "已赞"
                    )
                )
            ) {
                return@next Step.get(StepTag.STEP_31)
            } else {
                if (GestureUtils.click(
                        dataElement = DataElement(
                            className = NodeClassValue.Button,
                            descStart = "赞,"
                        )
                    )
                ) {
                    return@next Step.get(StepTag.STEP_31)
                }
            }
            return@next Step.repeat
        }.next(StepTag.STEP_31) {
            while (true) {
                if (GestureUtils.existElement(
                        dataElement = DataElement(
                            className = NodeClassValue.TextView,
                            text = "头条AI"
                        )
                    )
                ) {
                    return@next Step.get(StepTag.STEP_2)
                }
                AssistsCore.back()
                GestureUtils.widgetDelay(start = 1000, end = 2000)
            }
            return@next Step.none
        }.next(StepTag.STEP_100) {
            AssistsCore.home()
            return@next Step.none
        }
    }
}